function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}


// Show-Hide Layer
function showLayer(tgtEl) {document.getElementById(tgtEl).style.display = "block"; }
function hideLayer(tgtEl) {document.getElementById(tgtEl).style.display = "none"; }
function showhideLayer(tgtEl) {
  var el = document.getElementById(tgtEl);
  el.style.display = (el.style.display == "none" ? "block" : "none");
}

// alert(msg)
function message(msg) {
  alert(msg);
}
// 자식 엘리먼트 선택
function myNextSibling(obj) {
  var childElem = obj.childNodes;
  for(var i=0; i < childElem.length; i++) {
    if(childElem[i].nodeType == 1) {
      var mnsObj = childElem[i];
      return mnsObj;
      break;
    }
  }
}

/* dialog */
var popup = {
  layerObject : new Array(),
  eventTargtObject : null,
  close : function(layerID) {
    if (popup.layerObject[layerID] != null && popup.layerObject[layerID] != undefined) {
      // 이벤트 제거할 객체 정의
      popup.eventTargetObject = popup.layerObject[layerID];
      // 사용자 정의 함수 실행
      popup.eventTargetObject.hide();
      popup.eventTargetObject.close();
    }
  },
  openDialog : function(layerID, layerWidth, layerHeight, autoHeight, modal, isClose) {
   if(modal == undefined) modal = false;
    if(isClose == undefined) isClose = true;   // isClose 매개변수를 넘기지 않았을경우의 기본값처리..
    if (popup.layerObject[layerID] == undefined || null == popup.layerObject[layerID]) {
      popup.layerObject[layerID] = new Ext.Window({
        el: layerID,
        autoScroll: true,
        shadow: false,
        autoHeight:autoHeight,
        width: layerWidth,
        height: layerHeight,
        closeAction: 'hide',
        closable:isClose,  // isClose 값이 false 일 경우에 버튼값이 보이지 않습니다.
        modal: modal,
        on:function() {
          pop_scroll(layerID, layerHeight, autoHeight);
        }
      });
    }
    popup.layerObject[layerID].show();
  },
  setPosition : function(layerID, left, top) {
   popup.layerObject[layerID].setPosition(left, top);
  }
};

function pop_scroll(layerID, height, autoHeight) {
  var topHeight = 44;
  var bottomHeight = 25;
  var totalHeight = height;
  var contHeight = totalHeight - topHeight - bottomHeight;
  var popElem = document.getElementById(layerID);
  var divsions = popElem.getElementsByTagName("div");
  var h1Elem = popElem.getElementsByTagName("h1");
  for(var i=0; i < divsions.length; i++) {
    if(divsions[i].className != "x-window-body") continue;
    var xWinBody = divsions[i];
    var contElem = myNextSibling(xWinBody);
    if((xWinBody.scrollHeight > contHeight) && (!autoHeight)) {
      contElem.style.width = (parseInt(contElem.style.width) - 32) + "px";
      h1Elem[0].style.margin = "0 15px 0 0"
    } else {
      contElem.style.width = (parseInt(contElem.style.width) - 17) +"px";
    }
  }
}

